package service

import (
	"fmt"
	"shopping_car/common"
)

var tmp = make(map[ShoppingGoods]int)

func initGoods(goodsName string, price int) ShoppingGoods {
	goods := ShoppingGoods{
		Price: price,
		Goods: goodsName,
		Count: 1,
	}
	return goods
}

func printGoods() []ShoppingGoods {
	// 暂时先写死，后台管理功能先不写
	goods1 := initGoods("特斯拉model 3", 2000)
	goods2 := initGoods("马自达-昂克赛拉", 1500)
	goods3 := initGoods("丰田-卡罗拉", 1000)
	goods4 := initGoods("福特-野马", 3000)

	var shoppingGoods []ShoppingGoods
	shoppingGoods = append(shoppingGoods, goods1, goods2, goods3, goods4)
	for k := range shoppingGoods {

		fmt.Printf("编号：%-10d 商品：%-32s 价格%d\n", k, shoppingGoods[k].Goods, shoppingGoods[k].Price)

	}
	return shoppingGoods
}

func (cs *Consumer) Shopping() {
	Goods := printGoods()
	for {
		fmt.Printf("请输入添加购物车的商品编号：")
		var choiseNum int
		fmt.Scan(&choiseNum)
		if choiseNum >= len(Goods) {
			fmt.Println("您输入的商品无效，请重新输入！")
			continue
		}

		tmp[Goods[choiseNum]]++

		fmt.Println()
		fmt.Printf("商品 < %s > 添加成功\n", Goods[choiseNum].Goods)
		fmt.Println()
		tmpSlice := []ShoppingGoods{}
		for k, v := range tmp {

			tmpGoods := ShoppingGoods{}
			tmpGoods.Goods = k.Goods
			tmpGoods.Count = v
			tmpGoods.Price = k.Price
			tmpSlice = append(tmpSlice, tmpGoods)
		}
		cs.ShoppingCar = tmpSlice

		if common.Isback() {
			cs.SaveData(*cs)
			break
		}
	}
}

func (cs *Consumer) ListGoods() {
	for i := range cs.ShoppingCar {
		count := cs.ShoppingCar[i].Count
		goods := cs.ShoppingCar[i].Goods
		price := cs.ShoppingCar[i].Price
		fmt.Printf("商品名称：%-32s 商品单价：%-8d 商品数量：%-8d 合计价格：%-8d\n", goods, price, count, count*price)
	}

}
